package com.zte.claa.inficombo.csa.app.model.redis;

import java.sql.Timestamp;
import java.util.UUID;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.zte.claa.inficombo.csa.app.util.AlarmUtils;

public class AlarmData {
	public static final String DTFormat = "yyyy-MM-dd HH:mm:ss";
	public static final String TIME_ZONE = "GMT+8";
	
	public static final byte ALARM_LEVEL_0 = 0;
	public static final byte ALARM_LEVEL_1 = 1;
	public static final byte ALARM_LEVEL_2 = 2;
	public static final byte ALARM_LEVEL_3 = 3;
	public static final byte ALARM_LEVEL_4 = 4;
	
	/**
	 * 0: 告警恢复；1: 告警发生；2: 通知消息；
	 */
	public static final byte TPFLAG_CLEAR = 0;
	public static final byte TPFLAG_ALARM = 1;	
	public static final byte TPFLAG_INFOM = 2;
	
	public static final byte CONFIRM_STATE_N = 0;	
	public static final byte CONFIRM_STATE_Y = 1;	
	
	private String alarmkey;    // 告警键值：deveui@devtype@alarmcode;
	private String alarmid; 	//	告警流水号	VARCHAR(200)
	private byte alarmlevel; 	//	告警级别	Byte
	private String alarmcode; 	//	告警码	VARCHAR(100)，对于北秦这种复杂场景，CSA发布的告警消息可能一次包含多个code，分多条告警上报.
	private byte typeflag; 		//	类型标记	Byte
	private String title; 		//		告警标题	VARCHAR(100)
	private String deveui; 		//		告警设备标识	VARCHAR(50)
	private String devtype; 	//		告警设备类型	VARCHAR(100)
	private String projectid;   //      工程标识  VARCHAR(200)
	
    @JsonFormat(pattern=DTFormat, timezone = TIME_ZONE)
	private Timestamp alarmtime; 	//	告警发生时间	Timestamp
	private String descp; 			//		告警详细描述	VARCHAR(200)
	
	@JsonFormat(pattern=DTFormat, timezone = TIME_ZONE)
	private Timestamp cleartime; 	//	告警恢复时间	Timestamp	
	@JsonIgnore
	private byte confirmstate; 		//	告警确认状态	Byte	
	@JsonIgnore
	private Timestamp confirmtime; 	//	告警确认时间	Timestamp
	@JsonIgnore
	private String confirmer; 		//	告警确认人	VARCHAR(50)
	
	private String remark;
	// 增加devname，方便ffhost等场景中alarm的devname设置.
	private String devname;
	 
	public AlarmData(){
		this.alarmid = "A"+Math.abs(UUID.randomUUID().getLeastSignificantBits());
		this.alarmlevel = ALARM_LEVEL_1;
		this.confirmstate = CONFIRM_STATE_N;
		this.descp = "";
	}

	public String getAlarmid() {
		return alarmid;
	}

	public void setAlarmid(String alarmid) {
		this.alarmid = alarmid;
	}
	
    public String getProjectid() {
        return projectid;
    }

    public void setProjectid(String projectid) {
        this.projectid = projectid;
    }
	

	public byte getAlarmlevel() {
		return alarmlevel;
	}

	public void setAlarmlevel(byte alarmlevel) {
		this.alarmlevel = alarmlevel;
	}

	public String getAlarmcode() {
		return alarmcode;
	}

	public void setAlarmcode(String alarmcode) {
		this.alarmcode = alarmcode;
	}

	public byte getTypeflag() {
		return typeflag;
	}

	public void setTypeflag(byte typeflag) {
		this.typeflag = typeflag;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getDeveui() {
		return deveui;
	}

	public void setDeveui(String deveui) {
		this.deveui = deveui;
	}

	public String getDevtype() {
		return devtype;
	}

	public void setDevtype(String devtype) {
		this.devtype = devtype;
	}

	public Timestamp getAlarmtime() {
		return alarmtime;
	}

	public void setAlarmtime(Timestamp alarmtime) {
		this.alarmtime = alarmtime;
	}

	public String getDescp() {
		return descp;
	}

	public void setDescp(String descp) {
		this.descp = descp;
	}

	public Timestamp getCleartime() {
		return cleartime;
	}

	public void setCleartime(Timestamp cleartime) {
		this.cleartime = cleartime;
	}

	public byte getConfirmstate() {
		return confirmstate;
	}

	public void setConfirmstate(byte confirmstate) {
		this.confirmstate = confirmstate;
	}

	public Timestamp getConfirmtime() {
		return confirmtime;
	}

	public void setConfirmtime(Timestamp confirmtime) {
		this.confirmtime = confirmtime;
	}

	public String getConfirmer() {
		return confirmer;
	}

	public void setConfirmer(String confirmer) {
		this.confirmer = confirmer;
	}

	public void setAlarmTimeWithCurTime(){
		this.alarmtime = new Timestamp(System.currentTimeMillis());
	}
	
	public void setClearTimeWithCurTime(){
		this.cleartime = new Timestamp(System.currentTimeMillis());
	}

	/**
	 * @return the remark
	 */
	public String getRemark() {
		return remark;
	}

	/**
	 * @param remark the remark to set
	 */
	public void setRemark(String remark) {
		this.remark = remark;
	}

	public String getDevname() {
		return devname;
	}

	public void setDevname(String devname) {
		this.devname = devname;
	}

	public String toString(){
		StringBuilder sb = new StringBuilder();
		sb.append("alarmid=").append(alarmid).append(", ");
		sb.append("alarmkey=").append(alarmkey).append(", ");
		sb.append("projectid=").append(projectid).append(", ");
		sb.append("alarmlevel=").append(alarmlevel).append(", ");
		sb.append("alarmcode=").append(alarmcode).append(", ");
		sb.append("typeflag=").append(typeflag).append(", ");
		sb.append("title=").append(title).append(", ");
		sb.append("deveui=").append(deveui).append(", ");
		sb.append("devname=").append(devname).append(", ");
		sb.append("remark=").append(remark).append(", ");
		sb.append("alarmtime=").append(alarmtime).append(".");
		return sb.toString();
	}

	public String getAlarmkey() {
		if(alarmkey == null){
			alarmkey = AlarmUtils.generateAlarmKey(this.deveui, this.devtype, this.alarmcode);
		}
		return alarmkey;
	}

	public void setAlarmkey(String alarmkey) {
		this.alarmkey = alarmkey;
	}
}
